<template>
  <div v-if="hasAccess">
    <slot />
  </div>
  <div v-else class="access-denied">
    <div class="access-denied-content">
      <div class="access-denied-icon">
        <svg width="64" height="64" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <circle cx="12" cy="12" r="10" stroke="#EF4444" stroke-width="2"/>
          <path d="M8 12L16 12" stroke="#EF4444" stroke-width="2" stroke-linecap="round"/>
        </svg>
      </div>
      <h2 class="access-denied-title">访问被拒绝</h2>
      <p class="access-denied-message">{{ message }}</p>
      <div class="access-denied-actions">
        <button @click="goBack" class="btn-secondary">返回</button>
        <button @click="goHome" class="btn-primary">回到主页</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuth } from '@/composables/useAuth'
import { usePermissions } from '@/composables/usePermissions'

interface Props {
  permission?: string
  roles?: string[]
  fallbackMessage?: string
}

const props = withDefaults(defineProps<Props>(), {
  fallbackMessage: '您没有访问此页面的权限'
})

const router = useRouter()
const { userRole, isAuthenticated } = useAuth()
const { hasPermission, PermissionManager } = usePermissions()

// 检查是否有访问权限
const hasAccess = computed(() => {
  // 如果未登录，不允许访问
  if (!isAuthenticated.value) {
    return false
  }

  // 如果指定了权限，检查权限
  if (props.permission) {
    return hasPermission(props.permission).value
  }

  // 如果指定了角色，检查角色
  if (props.roles && props.roles.length > 0) {
    return userRole.value && props.roles.includes(userRole.value)
  }

  // 默认允许访问（如果没有指定权限或角色）
  return true
})

// 错误消息
const message = computed(() => {
  if (!isAuthenticated.value) {
    return '请先登录以访问此页面'
  }
  
  if (props.permission) {
    return `您需要 ${props.permission} 权限才能访问此页面`
  }
  
  if (props.roles && props.roles.length > 0) {
    return `此页面仅限 ${props.roles.join('、')} 访问`
  }
  
  return props.fallbackMessage
})

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 回到主页
const goHome = () => {
  const { getUserHomePage } = useAuth()
  router.push(getUserHomePage())
}
</script>

<style scoped>
.access-denied {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
  padding: 20px;
}

.access-denied-content {
  text-align: center;
  max-width: 400px;
}

.access-denied-icon {
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
}

.access-denied-title {
  font-size: 24px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 12px;
}

.access-denied-message {
  font-size: 16px;
  color: #6B7280;
  margin-bottom: 24px;
  line-height: 1.5;
}

.access-denied-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.btn-primary {
  background: #3B82F6;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary:hover {
  background: #2563EB;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.btn-secondary {
  background: #F3F4F6;
  color: #374151;
  border: 1px solid #D1D5DB;
  border-radius: 8px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  background: #E5E7EB;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

@media (max-width: 640px) {
  .access-denied-actions {
    flex-direction: column;
  }
  
  .btn-primary,
  .btn-secondary {
    width: 100%;
  }
}
</style>
